package cn.yunyichina.provider.hisiface.invoke;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.yunyichina.provider.hisiface.http.HttpServerConfig;

/**
 * 对外接口服务实例工厂
 * 
 * @author zhou
 *
 */
public class OutsideInvokeServiceFactory {
	
	private static final Logger logger = LoggerFactory.getLogger(OutsideInvokeServiceFactory.class);

	public static IOutsideInvoke getInstance(String className) {
		IOutsideInvoke service = null;
		try {
			// service = (IOutsideInvoke) SpringContextHandler.getBean(ClassLoader.getSystemClassLoader().loadClass(className));
			service = (IOutsideInvoke) Class.forName(className).newInstance();
		} catch (Exception e) {
			logger.error("Class: [" + className + "] 实例化失败", e);
		}
		
		return service;
	}
	
	private static final Map<String, String> openserviceMap = new HashMap<>();
	
	public static Map<String, String> getOpenserviceMap() {
		return openserviceMap;
	}
	
	/**
	 * 初始化对外接口服务配置
	 */
	public static void initOpenserviceMap() throws Exception {
		try (InputStream is = HttpServerConfig.class.getClassLoader().getResourceAsStream("conf/openservice.properties")) {
			Properties pro = new Properties();
	        pro.load(is);
	        Set<Entry<Object, Object>> entrySet = pro.entrySet();
	        for (Entry<Object, Object> entry : entrySet) {
	        	String key = (String) entry.getKey();
	        	String value = (String) entry.getValue();
	        	openserviceMap.put(key, value);
	        	logger.info("初始化对外接口服务配置: [{} = {}]", key, value);
	        }
		}
	}
}
